package com.neulogistics.core.substation.api.application.integrationevent.handler;

import com.blackto.lib.eventbus.abstraction.IIntegrationEventHandler;
import com.neulogistics.core.substation.api.application.integrationevent.subscribe.PickUpOutboundIntegrationEvent;
import com.neulogistics.core.substation.domain.exception.SubstationDomainException;
import com.neulogistics.core.substation.domain.model.aggregate.TaskOrder;
import com.neulogistics.core.substation.domain.model.enumeration.TaskStatus;
import com.neulogistics.core.substation.domain.repository.ITaskOrderRepository;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@Component
public class PickUpOutboundIntegrationEventHandler implements IIntegrationEventHandler<PickUpOutboundIntegrationEvent> {
    @Resource
    ITaskOrderRepository taskOrderRepository;

    @Override
    public CompletableFuture<Void> handle(PickUpOutboundIntegrationEvent event) throws SubstationDomainException {
        // 根据任务单id查询任务单并修改状态
        TaskOrder taskOrder = taskOrderRepository.findById(event.taskId);
        if(taskOrder.getTaskStatus() != TaskStatus.To_Be_Picked){
            throw new SubstationDomainException(ErrorCode.OPERATION_ERROR, "错误的任务单状态");
        }

        taskOrder.setTaskStatus(TaskStatus.To_Be_Delivery);
        taskOrderRepository.save(taskOrder);

        return CompletableFuture.completedFuture(null);
    }
}
